﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdverseEventReportSystem.Utils {
	public static class ComboBoxHelper {
		#region 根据DataTable获取下拉列表
		/// <summary>
		/// 根据DataTable获取下拉列表 (text字段名称为"Text"，value字段名称为"ID"，有请选择，且Value为0)
		/// </summary>
		/// <param name="dt"></param>
		/// <returns></returns>
		public static List<CListItem> GetList(DataTable dt) {
			string textFieldName = "Text";
			string valueFieldName = "ID";
			return GetList(dt, textFieldName, valueFieldName);
		}
		/// <summary>
		/// 根据DataTable获取下拉列表 (有请选择，且Value为0)
		/// </summary>
		/// <param name="dt">DataTable数据源</param>
		/// <param name="textFieldName">text字段名称</param>
		/// <param name="valueFieldName">value字段名称</param>
		/// <returns></returns>
		public static List<CListItem> GetList(DataTable dt, string textFieldName, string valueFieldName) {
			bool needPleaseSelect = true;
			string firstDefaultValue = "0";
			string firstDefaultText = "请选择";
			return GetList(dt, textFieldName, valueFieldName, needPleaseSelect, firstDefaultValue, firstDefaultText);
		}
		/// <summary>
		/// 根据DataTable获取下拉列表
		/// </summary>
		/// <param name="dt">DataTable数据源</param>
		/// <param name="textFieldName">text字段名称</param>
		/// <param name="valueFieldName">value字段名称</param>
		/// <param name="needPleaseSelect">是否需要“请选择”</param>
		/// <param name="firstDefaultValue">”请选择“的默认Value 如果不需要“请选择”传空</param>
		/// <param name="firstDefaultText">”请选择“的默认Text 如果不需要“请选择”传空</param>
		/// <returns></returns>
		public static List<CListItem> GetList(DataTable dt, string textFieldName, string valueFieldName, bool needPleaseSelect, string firstDefaultValue, string firstDefaultText) {
			List<CListItem> list = new List<CListItem>();
			if (needPleaseSelect) {
				CListItem item = new CListItem();
				item.Text = firstDefaultText;
				item.Value = firstDefaultValue;
				list.Add(item);
			}
			for (int i = 0; i < dt.Rows.Count; i++) {
				CListItem item = new CListItem();
				item.Text = dt.Rows[i][textFieldName].ToString();
				item.Value = dt.Rows[i][valueFieldName].ToString();
				list.Add(item);
			}
			return list;
		}
		#endregion
	}
}
